import pygame
from sys import exit
from pygame.locals import *
import os
import random

def gameover():
    game = pygame.image.load("imagens" + os.sep + "game.png").convert()
    screen.blit(game, (0, 0))
    pygame.display.update()

pygame.init()
screen_width, screen_height = 640, 480
screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)
middle = screen_width / 2, screen_height / 2
pygame.display.set_caption("DrunkSnake - JacaGames")


pygame.mixer.music.load("sounds" + os.sep + "rebolation.mp3")
pygame.mixer.music.play()
play_music = True


snake = pygame.image.load("imagens" + os.sep + "cobra.png").convert_alpha()
food = pygame.image.load("imagens" + os.sep + "bebida.png").convert_alpha()
fundo = pygame.image.load("imagens" + os.sep + "cenario1.gif").convert()




white = (255, 255, 255)
black = (0, 0, 0)


clock = pygame.time.Clock()

#coordenadas da snake
x, y = 320, 240

#coordenadas da comida
x_food = random.randint(12 , 620)
y_food = random.randint(12 , 460)
cont = 1
x_corpo = 30
move_x, move_y = 0, 0
lista = []
while True:
	
    clock.tick(100)
    ret_food = Rect(x_food, y_food, 20,20)
    ret_snake = Rect(x, y, 20,20)
    
    for event in pygame.event.get():
        #saida sem erro
        if event.type == QUIT:
            exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                move_y = 0
                move_x = -1
            elif event.key == K_RIGHT:
                move_y = 0
                move_x = +1
            elif event.key == K_UP:
                move_x = 0
                move_y = -1
            elif event.key == K_DOWN:
                move_x = 0
                move_y = +1
                
    #atualiza as coordenadas da snake 
    x += move_x
    y += move_y
	
    
    screen.blit(fundo, (0, 0))
    
    screen.blit(snake, (x, y))
    
    #limite das paredes
    if x < 29:
        gameover()
        break
    elif x > 650:
        gameover()
        break
    elif y < 20:
        gameover()
        break
    elif y > 450:
        gameover()
        break


    #pausar musica
    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[K_p]:
        if play_music:
            pygame.mixer.music.pause()
            play_music = False
        else:
            pygame.mixer.music.unpause()
            play_music = True


	
    screen.blit(food, (x_food, y_food))
    pygame.display.update()
    
    if ret_snake.colliderect(ret_food):
		x_food = random.randint(30 , 620)
		y_food = random.randint(30 , 460)
		
    

